package com.endpoint.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.endpoint.common.bean.UserDetails;
import com.endpoint.common.utils.HttpUtils;
import com.endpoint.common.utils.JwtTokenUtil;
import com.endpoint.member.entity.Member;
import com.endpoint.member.service.MemberService;
import com.endpoint.member.service.OAuth2Service;
import com.endpoint.member.vo.SocialUserVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Project: endpoint
 * @Name: OAuth2ServiceImpl
 * @Author: Jack_Li
 * @Date: 2024/7/6 13:52
 */
@Service
public class OAuth2ServiceImpl implements OAuth2Service {

    @Autowired
    private MemberService memberService;

    @Override
    public UserDetails oauthLogin(SocialUserVo socialUserVo) {
        String uid = socialUserVo.getUid();
        //判断当前 社交登录用户是否 登录过系统
        Member member = memberService.getOne(new QueryWrapper<Member>().eq("social_uid", uid));
        if(member != null){
            member.setSocialUid(socialUserVo.getUid());
            member.setAccessToken(socialUserVo.getAccess_token());
//            member.setExpiresIn(socialUserVo.getExpires_in());
            member.setUpdateTime(new Date());
            //更新用户 社交登录数据
            memberService.updateById(member);

            UserDetails userDetails = new UserDetails();
            //生成 jwtToken
            String token = JwtTokenUtil.createToken(member.getId());
            userDetails.setId(member.getId());
            userDetails.setNickName(member.getNickName());
            userDetails.setToken(token);
            return userDetails;
        }else{
            //第一次社交登录保存用户信息到 member
            member = new Member();
            try {
                // 查询当前社交用户的社交账号信息
                Map<String,String> query = new HashMap<>();
                query.put("access_token",socialUserVo.getAccess_token());
                query.put("uid",uid);
                // 查询成功
                // 昵称
                member.setNickName(socialUserVo.getIsRealName());
                // 性别

                //头像
                member.setHeader(socialUserVo.getAvatar_url());
                //填充其他数据
                member.setSocialUid(socialUserVo.getUid());
                member.setAccessToken(socialUserVo.getAccess_token());
                member.setCreateTime(new Date());
                member.setUpdateTime(new Date());
                memberService.save(member);

                //保存成功生成令牌
                UserDetails userDetails = new UserDetails();
                //生成 jwtToken
                String token = JwtTokenUtil.createToken(member.getId());
                userDetails.setId(member.getId());
                userDetails.setNickName(member.getNickName());
                userDetails.setToken(token);
                return userDetails;
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

}
